package com.qipay.admin.holiday.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.holiday.service.HolidayLogicService;
import com.qipay.excel.service.ExcelUtils;
import com.qipay.resource.entity.Resource;
import com.qipay.resource.service.ResourceService;
import com.qipay.utils.Result;
import com.qipay.utils.ResultCodeEnum;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.text.ParseException;

/**
 * Creator: Yao
 * Date:    2017/12/13
 * For:
 * Other:
 */
@Controller
@RequestMapping("/holiday")
public class HolidayController extends BaseController {

    @Autowired
    HolidayLogicService holidayService;

    @Autowired
    ExcelUtils excelUtils;
    @Autowired
    ResourceService resourceService;

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public ModelAndView show(HttpServletResponse resp) throws IOException, ParseException {

        return createModelAndView("holiday");
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(
            @RequestParam(value = "year", required = false) String year,
            HttpServletResponse resp) throws IOException, ParseException {

        excelUtils.download(resp, holidayService.export(year));
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> upload(@RequestBody @NotNull MultipartFile file) {

        // check
        Assert.notNull(file);
        String filename = file.getOriginalFilename();
        Assert.isTrue(StringUtils.isNotBlank(filename));
        String extension = FilenameUtils.getExtension(filename);
        Assert.isTrue(StringUtils.isNotBlank(extension));
        Assert.isTrue(extension.startsWith("xls"));

        // init
        Result<String> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());

        // handle
        try {
            Resource resource = resourceService.upload(file);
            holidayService.upload(resource.getFilePath());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultCodeEnum.SUCCESS.getCode());
            result.setMsg(e.getMessage());
        }

        return result;
    }


    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Result<Boolean> check(@RequestParam(value = "date", required = false) String date) {

        // init
        Result<Boolean> result = new Result<>();

        // check
        if (StringUtils.isBlank(date)) {

            result.setCode(ResultCodeEnum.FAIL.getCode());
            result.setMsg("date is empty");
            return result;
        }

        try {
            result.setData(holidayService.check(date));
            result.setCode(ResultCodeEnum.SUCCESS.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultCodeEnum.FAIL.getCode());
            result.setMsg(e.getMessage());
        }

        return result;
    }
}
